﻿Public Class GameControl
    Public Property GameName As String
        Get
            Return NameLabel.Text.Remove(NameLabel.Text.IndexOf("(") - 1)
        End Get
        Set(ByVal value As String)
            NameLabel.Text = value
        End Set
    End Property

    Sub New(ByVal GameName As String)
        InitializeComponent()
        NameLabel.Text = GameName & " (0)"
        'GameScoreboard = New Scoreboard(GameName)
    End Sub

    Friend ActionsToInvoke As New Dictionary(Of System.Delegate, Object)

    Friend Sub InvokeDelayedActions()
        For Each item As KeyValuePair(Of System.Delegate, Object) In ActionsToInvoke
            Invoke(item.Key, item.Value)
        Next
        ActionsToInvoke.Clear()
    End Sub

    Dim ConsoleLines As New List(Of String)
    Dim LastDetailStatus As String
    Dim LastDetail As String
    Public Sub SetStatus(ByVal Status As String, Optional ByVal Detail As String = "")
        If LastDetailStatus = Status Then
            With ConsoleLines
                .Item(.LastIndexOf(Status & " (" & LastDetail & ")")) = Status & " (" & Detail & ")"
            End With
        Else
            If Detail = "" Then
                ConsoleLines.Add(Status)
            Else
                ConsoleLines.Add(Status & " (" & Detail & ")")
            End If
        End If
        If ConsoleLines.Count = 51 Then
            ConsoleLines.RemoveAt(0)
        End If

        SetConsoleLines()

        If Not Detail = "" Then
            LastDetailStatus = Status
            LastDetail = Detail
        End If
    End Sub

    Public Sub SetConsoleLines()
        If InvokeRequired Then
            Try
                Invoke(New Action(AddressOf SetConsoleLines))
            Catch
            End Try
            Exit Sub
        End If
        Console.Lines = ConsoleLines.ToArray
        Console.SelectionStart = Console.Text.Length
        Console.ScrollToCaret()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If MsgBox("Are you sure you would like to close this game?" & vbCr & "WARNING: This will cause any clients connected to generate errors.", MsgBoxStyle.YesNo, "Close Game") = MsgBoxResult.Yes Then
            Dim ID As Integer
            With ServerMod.GameServer
                ID = .GameNames(GameName)
                ._EndGame(ID)
                .Games.Remove(ID)
                .GameNames.Remove(GameName)
            End With
            'ServerMod.GameServer_EndGame(ID)
        End If
    End Sub

    Sub EnableCtrlButton(ByVal Enable As Boolean)
        If InvokeRequired Then
            Try
                Invoke(New Action(Of Boolean)(AddressOf EnableCtrlButton), Enable)
            Catch
                ActionsToInvoke.Add(New Action(Of Boolean)(AddressOf EnableCtrlButton), Enable)
            End Try
            Exit Sub
        End If
        CtrlButton.Enabled = Enable
    End Sub

    Sub CheckAllowHost(ByVal Checked As Boolean)
        If InvokeRequired Then
            Try
                Invoke(New Action(Of Boolean)(AddressOf CheckAllowHost), Checked)
            Catch
                ActionsToInvoke.Add(New Action(Of Boolean)(AddressOf CheckAllowHost), Checked)
            End Try
            Exit Sub
        End If
        AllowHost.Checked = Checked
    End Sub

    Private Sub ClientUpdateTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClientUpdateTimer.Tick
        Try
            NameLabel.Text = GameName & " (" & GameServer.Games(GameServer.GameNames(GameName)).Clients.Count & ")"
        Catch
        End Try
    End Sub

    Private Sub GameControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ClientUpdateTimer.Start()
    End Sub

    Sub UpdateScores(ByVal Scores As SortedList(Of String, Long))
        If InvokeRequired Then
            Invoke(New Action(Of SortedList(Of String, Long))(AddressOf UpdateScores), Scores)
            Exit Sub
        End If
        For Each User As String In Scores.Keys
            For Each Item As ListViewItem In Board.Items
                If Item.Text = User Then
                    Item.SubItems.Item(1).Text = Scores(User)
                    GoTo nextuser
                End If
            Next
            Board.Items.Add(User).SubItems.Add(Scores(User))
nextuser: Next
        Board.ListViewItemSorter = New ListViewComparer
        Board.Sort()
    End Sub
End Class

Class ListViewComparer
    Implements IComparer

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        Try
            Dim returnVal As Integer = -1
            returnVal = 1 - Decimal.Compare(CType(x, ListViewItem).SubItems(1).Text, CType(y, ListViewItem).SubItems(1).Text)
            Return returnVal
        Catch
            Return 0
        End Try
    End Function
End Class
